<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备编号" prop="barrierNo">
      <el-input v-model="dataForm.barrierNo" placeholder="设备编号"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="barrierName">
      <el-input v-model="dataForm.barrierName" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="所在位置" prop="barrierLocation">
      <el-input v-model="dataForm.barrierLocation" placeholder="所在位置"></el-input>
    </el-form-item>
    <el-form-item label="控制状态" prop="barrierStatus">
      <el-input v-model="dataForm.barrierStatus" placeholder="控制状态"></el-input>
    </el-form-item>
    <el-form-item label="维修人员" prop="linkMan">
      <el-input v-model="dataForm.linkMan" placeholder="维修人员"></el-input>
    </el-form-item>
    <el-form-item label="联系号码" prop="linkPhone">
      <el-input v-model="dataForm.linkPhone" placeholder="联系号码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/carbarrier'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          barrierNo: '',

          barrierName: '',

          barrierLocation: '',

          barrierStatus: '',

          linkMan: '',

          linkPhone: '',

          createTime: '',

          updateTime: '',

          delFlag: '',

        },
        dataRule: {
          barrierNo: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ],

          barrierName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],

          barrierLocation: [
            { required: true, message: '所在位置不能为空', trigger: 'blur' }
          ],

          barrierStatus: [
            { required: true, message: '控制状态不能为空', trigger: 'blur' }
          ],

          linkMan: [
            { required: true, message: '维修人员不能为空', trigger: 'blur' }
          ],

          linkPhone: [
            { required: true, message: '联系号码不能为空', trigger: 'blur' }
          ],

          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],

          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],

          delFlag: [
            { required: true, message: '0 正常 其余删除不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
